-- ~~| Libraries |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local DiesalGUI 	= LibStub('DiesalGUI-1.0')
local DiesalStyle = LibStub("DiesalStyle-1.0")
local DiesalTools = LibStub('DiesalTools-1.0')
-- ~~| Lua Upvalues |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local tostring										= tostring
local format 										= string.format
-- ~~| WoW Upvalues |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~| ConfigToggle |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local Type 		= 'OPTspinner'
local Version 	= 1
-- ~~| ConfigToggle StyleSheets |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local styleSheet = {
	['name-color'] = {
		type			= 'Font',
		color			= 'bfbfbf',
	},	
}
-- ~~| ConfigToggle Locals |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~| ConfigToggle Methods |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local methods = {
	['OnAcquire'] = function(self)
		self:AddStyleSheet(styleSheet)
		-- self:AddStyleSheet(wireFrameSheet)
		self:Show()
		self.checkBox:Hide()
	end,
	['OnRelease'] = function(self)

	end,
	['ApplySettings'] = function(self)
		local optionTable = self.settings.optionTable
		self:SetHeight(18)
		if self.settings.position == 1 then
			self:SetPoint('TOPLEFT',0,-self.settings.padTop)
			self:SetPoint('RIGHT')
		else
			self:SetPoint('TOPLEFT',self.settings.parentObject.children[self.settings.position-1].frame,'BOTTOMLEFT',0,0)
			self:SetPoint('RIGHT')
		end

		self.name:SetText(optionTable.name)
		if optionTable.toggle ~= false then self.checkBox:Show() end
		self.spinner:SetSettings({
			min		= optionTable.min 		or 0,
			max		= optionTable.max 		or 100,
			step		= optionTable.step 		or 5,
			width		= optionTable.width and DiesalTools:Round(optionTable.width,2)-1 or 50	,
		},true)
	end,
	['SetDB'] = function(self,db,moduleDB)
		self.settings.db 			= db
		self.settings.moduleDB 	= moduleDB

		if self.settings.optionTable.toggle ~= false then
			self.checkBox:SetChecked(db.enable)
			if self.settings.optionTable.set then self.settings.optionTable.set('toggle',db.enable) end
			self:FireEvent('OnOptionUpdate',self.settings.optionTable.name,'toggle',db.enable)		
		end
		self.spinner:SetNumber(db.value)
		if self.settings.optionTable.set then self.settings.optionTable.set('spinner',db.value) end	
		self:FireEvent('OnOptionUpdate',self.settings.optionTable.name,'spinner',db.value)	
	end,
	['Lock'] = function(self,lock)
		self.checkBox:Disable()
		self.spinner:EnableMouse(false)
	end,
	['Unlock'] = function(self,lock)
		self.checkBox:Enable()
		self.spinner:EnableMouse(true)
	end,
}
-- ~~| ConfigToggle Constructor |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local function Constructor()
	local self 		= DiesalGUI:CreateObjectBase(Type)
	local frame		= CreateFrame('Frame',nil,UIParent)
	self.frame		= frame
	-- ~~ Default Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	self.defaults = { }
	-- ~~ Registered Events ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	-- ~~ Construct ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	local checkBox = DiesalGUI:Create('CheckBox')
	checkBox:SetParent(frame)
	checkBox:SetPoint("TOPLEFT",2,-3)
	checkBox:SetEventListener('OnValueChanged', function(this,event,value)
		self.settings.db.enable = value
		if self.settings.optionTable.set then self.settings.optionTable.set('toggle',value)	end
		self:FireEvent('OnOptionUpdate',self.settings.optionTable.name,'toggle',value)		
	end)
	checkBox:SetEventListener('OnEnter', function(this)
		if not self.settings.moduleDB.tooltips then return end
		GameTooltip:SetOwner(checkBox.frame, "ANCHOR_TOPLEFT",0,2)
		GameTooltip:AddLine('enable option')
		GameTooltip:Show()
	end)
	checkBox:SetEventListener('OnLeave', function(this)
		GameTooltip:Hide()
	end)

	local spinner = DiesalGUI:Create('Spinner')
	spinner:SetParent(frame)
	spinner:SetPoint('TOPRIGHT',-3,-1)
	spinner:SetEventListener('OnValueChanged', function(this,event,userInput,number)
		if not userInput then return end		
		self.settings.db.value = number					
		if self.settings.optionTable.set then self.settings.optionTable.set('spinner',number) end
		self:FireEvent('OnOptionUpdate',self.settings.optionTable.name,'spinner',number)	
	end)
	spinner:SetEventListener('OnEnter', function(this)
		if not self.settings.moduleDB.tooltips then return end
		GameTooltip:SetOwner(spinner.frame, "ANCHOR_TOPRIGHT",0,2)		
		GameTooltip:AddLine('|cff00aaffMouse Wheel:|r Adjust value by '..this.settings.step)		
		GameTooltip:Show()
	end)
	spinner:SetEventListener('OnLeave', function(this)
		GameTooltip:Hide()
	end)

	local nameFrame = self:CreateRegion("Frame", 'nameFrame', frame)
	nameFrame:SetHeight(14)
	nameFrame:SetPoint("TOPLEFT",16,-2)
	nameFrame:SetPoint("RIGHT",spinner.frame,'LEFT',-4,0)
	nameFrame:SetScript('OnEnter', function(this)
		if not self.settings.moduleDB.configTooltips then return end
		if not self.settings.optionTable.tooltip then return end
		GameTooltip:SetOwner(nameFrame, "ANCHOR_TOPLEFT",0,2)
		GameTooltip:AddLine('|cffff7f00'..self.settings.optionTable.tooltip,nil,nil,nil,1)
		GameTooltip:Show()
	end)
	nameFrame:SetScript('OnLeave', function(this)
		GameTooltip:Hide()
	end)

	local name = self:CreateRegion("FontString", 'name', nameFrame)
	name:SetPoint("TOPLEFT",0,-2)
	name:SetPoint("BOTTOMRIGHT",4,-2)
	name:SetJustifyV("TOP")
	name:SetJustifyH("LEFT")
	name:SetWordWrap(false)

	-- ~~ Frames ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	self.checkBox 	= checkBox
	self.spinner	= spinner
	-- ~~ Methods ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	for method, func in pairs(methods) do
		self[method] = func
	end
	-- ~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	DiesalGUI:RegisterObject(self)
	return self
end
DiesalGUI:RegisterObjectConstructor(Type,Constructor,Version)